package com.onpositive.wiki3.db.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.carrotsearch.hppc.IntOpenHashSet;
import com.onpositive.wiki3.db.ICategoryAccess;
import com.onpositive.wiki3.db.ITitleMapper;
import com.onpositive.wiki3.db.impl.CountingVocabalary.Pair;

public class CopyOfTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		WikiDBImpl wikiDBImpl = new WikiDBImpl("C:/work/content");
		ArrayList<Integer> path = new CategoryRefMap(wikiDBImpl, "Contents").getPath(wikiDBImpl.getTitleMapper().getId("T-34"));
		for (Integer i:path){
			String title = wikiDBImpl.getTitleMapper().getTitle(i);
			System.out.println(title);
		}
		int[] allCategoriesWithChilren = wikiDBImpl.getCategoryAccessor().getAllCategoriesWithChilren();
		int an=0;
		for (int a=0;a<allCategoriesWithChilren.length;a++){
			String title = wikiDBImpl.getTitleMapper().getTitle(allCategoriesWithChilren[a]);
			title=title.replace(' ','_').toLowerCase();
			if (title.endsWith("_by_type")){
				an++;
			}
			if (title.endsWith("_by_country")){
				an++;
			}
			if (title.endsWith("_by_continent")){
				an++;
			}
			if (title.endsWith("_by_location")){
				an++;
			}
			if (title.endsWith("_by_region")){
				an++;
			}
			if (title.endsWith("_by_period")){
				an++;
			}
			if (title.endsWith("_by_century")){
				an++;
			}
			if (title.endsWith("_by_year")){
				an++;
			}
			if (title.endsWith("_by_author")){
				an++;
			}
			if (title.endsWith("_by_nationality")){
				an++;
			}
			if (title.endsWith("_by_builder")){
				an++;
			}
			if (title.endsWith("_by_manufacturer")){
				an++;
			} 
			if (title.endsWith("types")){
				an++;
			}
			if (title.startsWith("wikipedia")){
				an++;
			}
			if (title.startsWith("talk")){
				an++;
			}
			if (title.startsWith("lists_of")){
				an++;
			}
			if (title.startsWith("indexes_of")){
				an++;
			}
			if (title.endsWith("lists")){
				an++;
			}
			if (title.startsWith("history")){
				an++;
			}
			if (title.startsWith("template")){
				an++;
			}
			if (title.endsWith("deaths")){
				an++;
			}
			if (title.endsWith("births")){
				an++;
			}
			if (title.endsWith("locomotives")){
				an++;
			}
			if (title.startsWith("estabilishments")){
				an++;
			}
			if (title.startsWith("defunct")){
				an++;
			}
			if (title.startsWith("fictional")){
				an++;
			}
			if (title.matches("(.)*\\d\\d\\d\\d(\\s)*-(\\s)*\\d\\d\\d\\d")){
				//System.out.println(title);
				an++;
			}
			
		}
		System.out.println(allCategoriesWithChilren.length);
		System.out.println(an);
//		int[] pages = wikiDBImpl.getTemlates().getPages(
//				wikiDBImpl.getTemlates().getTemplateNum("infobox_station"));
//		System.out.println(pages.length);
//		CategoryAccessImpl impl = (CategoryAccessImpl) wikiDBImpl
//				.getCategoryAccessor();
//		IntOpenHashSet m = new IntOpenHashSet();
//		for (int q : pages) {
//			m.add(q);
//		}
//		HashMap<Integer, IntOpenHashSet> parents = impl.parents(m);
//		CountingVocabalary vm = new CountingVocabalary();
//		for (int a = 0; a < pages.length; a++) {
//			IntOpenHashSet intOpenHashSet = parents.get(pages[a]);
//			if (intOpenHashSet != null) {
//				int[] parentCategories = intOpenHashSet.toArray();
//				for (int z : parentCategories) {
//					String title = wikiDBImpl.getTitleMapper().getTitle(z);
//					vm.add(title);
//				}
//			}
//		}
//		ArrayList<Pair> all = vm.all();
//
//		process(all, wikiDBImpl, vm,pages.length*20);

	}

	protected static void process(ArrayList<Pair> all, WikiDBImpl wikiDBImpl,
			CountingVocabalary vm,int max) {
		IntOpenHashSet mms = new IntOpenHashSet();
		for (Pair p : all) {
			int id = wikiDBImpl.getTitleMapper().getId(p.s);
			mms.add(id);
			
			System.out.println(p);
			if (p.i < 100) {
				break;
			}
		}
		CategoryAccessImpl impl = (CategoryAccessImpl) wikiDBImpl
				.getCategoryAccessor();
		HashMap<Integer, IntOpenHashSet> parents = impl.parents(mms);
		CountingVocabalary v0=vm;
		vm=new CountingVocabalary();
		for (int q:mms.toArray()) {
			IntOpenHashSet intOpenHashSet = parents.get(q);
			if (intOpenHashSet != null) {
				int[] parentCategories = intOpenHashSet.toArray();
				for (int z : parentCategories) {
					String title = wikiDBImpl.getTitleMapper().getTitle(z);
					vm.add(title,v0.get( wikiDBImpl.getTitleMapper().getTitle(q)));
				}
			}
		}
		all=vm.all();
		boolean found=false;
		for (Pair p : all) {
			System.out.println(p);
			if (p.i>=max){
				found=true;
			}
			if (p.i < 100) {
				break;
			}
		}
		if (!found){
		process(all, wikiDBImpl, vm, max);
		}
	}

	protected static void dos(WikiDBImpl wikiDBImpl, CategoryRefMap tt,
			String title) {
		int id = wikiDBImpl.getTitleMapper().getId(title);
		ArrayList<Integer> path = tt.getPath(id);
		for (int q : path) {
			System.out.println(wikiDBImpl.getTitleMapper().getTitle(q));
		}
	}

	static class Cats {
		public Cats(int[] cats, int[] dists) {
			super();
			this.cats = cats;
			this.dists = dists;
			this.parents = new int[dists.length];
		}

		public void fill(WikiDBImpl wikiDBImpl) {
			ITitleMapper titleMapper = wikiDBImpl.getTitleMapper();
			int id = titleMapper.getId("Contents");
			int level = 1;
			mark(id, 1);
			IntOpenHashSet ids = new IntOpenHashSet();
			ids.add(id);
			fill(wikiDBImpl, ids, level);

		}

		protected void fill(WikiDBImpl wikiDBImpl, IntOpenHashSet ids, int level) {
			// mark(id, level);
			ICategoryAccess categoryAccessor = wikiDBImpl.getCategoryAccessor();
			Map<Integer, IntOpenHashSet> categoryMembers = categoryAccessor
					.getCategoryMembers(ids, false);
			for (int id : categoryMembers.keySet()) {
				IntOpenHashSet empty = new IntOpenHashSet();
				IntOpenHashSet m = categoryMembers.get(id);
				for (int a = 0; a < cats.length; a++) {
					if (m.contains(cats[a])) {
						if (dists[a] == 0 || dists[a] > level) {
							dists[a] = level;
							parents[a] = id;
							empty.add(cats[a]);
							count++;
						}
					}
				}
				if (!empty.isEmpty()) {
					for (int q : empty.toArray()) {
						String title = wikiDBImpl.getTitleMapper().getTitle(q);
						System.out.println(title + ":" + level + ":" + count);
					}
				}
				if (!empty.isEmpty()) {
					fill(wikiDBImpl, empty, level + 1);
				}
			}
		}

		int[] cats;
		int[] dists;
		int[] parents;
		int count;

		public void mark(int id, int i) {
			for (int a = 0; a < cats.length; a++) {
				if (cats[a] == id) {
					dists[a] = i;
				}
			}
		}
	}

}
